home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / intrvews / tut-code.lha / tut-code / bvalue / main.c < prev   
Encoding:
C/C++ Source or Header  |  1992-01-04  |  4.5 KB  |  189 lines

  1. #include <IV-look/kit.h>
  2. #include <InterViews/adjust.h>
  3. #include <InterViews/background.h>
  4. #include <InterViews/box.h>
  5. #include <InterViews/glue.h>
  6. #include <InterViews/label.h>
  7. #include <InterViews/place.h>
  8. #include <InterViews/session.h>
  9. #include <InterViews/style.h>
  10. #include <InterViews/window.h>
  11. #include <OS/string.h>
  12. #include <stdio.h>
  13.  
  14. class App : public Adjuster {
  15. public:
  16.     App(Adjustable*);
  17.     virtual ~App();
  18.  
  19.     void print_value();
  20.     void continuous();
  21.  
  22.     virtual void update();
  23.     virtual void disconnect(Adjustable*);
  24. private:
  25.     Adjustable* adjustable_;
  26.     boolean continuous_;
  27. };
  28.  
  29. App::App(Adjustable* a) {
  30.     adjustable_ = a;
  31.     a->attach(Dimension_X, this);
  32.     continuous_ = false;
  33. }
  34.  
  35. App::~App() {
  36.     if (adjustable_ != nil) {
  37.     adjustable_->detach(Dimension_X, this);
  38.     }
  39. }
  40.  
  41. void App::print_value() {
  42.     printf("%.5f\n", adjustable_->cur_lower(Dimension_X));
  43. }
  44.  
  45. void App::continuous() {
  46.     continuous_ = !continuous_;
  47. }
  48.  
  49. void App::update() {
  50.     if (continuous_) {
  51.     print_value();
  52.     }
  53. }
  54.  
  55. void App::disconnect(Adjustable*) {
  56.     adjustable_ = nil;
  57. }
  58.  
  59. declare(ActionCallback,App);
  60. implement(ActionCallback,App);
  61.  
  62. class BoundedValue : public Adjustable {
  63. protected:
  64.     BoundedValue();
  65. public:
  66.     BoundedValue(Coord lower, Coord upper);
  67.     virtual ~BoundedValue();
  68.  
  69.     virtual void lower_bound(Coord);
  70.     virtual void upper_bound(Coord);
  71.     virtual void current_value(Coord);
  72.     virtual void scroll_incr(Coord);
  73.     virtual void page_incr(Coord);
  74.  
  75.     virtual Coord lower(DimensionName) const;
  76.     virtual Coord upper(DimensionName) const;
  77.     virtual Coord length(DimensionName) const;
  78.     virtual Coord cur_lower(DimensionName) const;
  79.     virtual Coord cur_upper(DimensionName) const;
  80.     virtual Coord cur_length(DimensionName) const;
  81.  
  82.     virtual void scroll_to(DimensionName, Coord position);
  83.     virtual void scroll_forward(DimensionName);
  84.     virtual void scroll_backward(DimensionName);
  85.     virtual void page_forward(DimensionName);
  86.     virtual void page_backward(DimensionName);
  87. private:
  88.     Coord curvalue_;
  89.     Coord lower_;
  90.     Coord span_;
  91.     Coord scroll_incr_;
  92.     Coord page_incr_;
  93. };
  94.  
  95. BoundedValue::BoundedValue() {
  96.     scroll_incr_ = 0.0;
  97.     page_incr_ = 0.0;
  98. }
  99.  
  100. BoundedValue::BoundedValue(Coord lower, Coord upper) {
  101.     lower_ = lower;
  102.     span_ = upper - lower;
  103.     scroll_incr_ = span_ * 0.04;
  104.     page_incr_ = span_ * 0.4;
  105.     curvalue_ = (lower + upper) * 0.5;
  106. }
  107.  
  108. BoundedValue::~BoundedValue() { }
  109.  
  110. void BoundedValue::lower_bound(Coord c) { lower_ = c; }
  111. void BoundedValue::upper_bound(Coord c) { span_ = c - lower_; }
  112.  
  113. void BoundedValue::current_value(Coord value) {
  114.     curvalue_ = value;
  115.     constrain(Dimension_X, curvalue_);
  116.     notify(Dimension_X);
  117.     notify(Dimension_Y);
  118. }
  119.  
  120. void BoundedValue::scroll_incr(Coord c) { scroll_incr_ = c; }
  121. void BoundedValue::page_incr(Coord c) { page_incr_ = c; }
  122.  
  123. #define access_function(name,value) \
  124. Coord BoundedValue::name(DimensionName) const { \
  125.     return value; \
  126. }
  127.  
  128. access_function(lower,lower_)
  129. access_function(upper,lower_ + span_)
  130. access_function(length,span_)
  131. access_function(cur_lower,curvalue_)
  132. access_function(cur_upper,curvalue_)
  133. access_function(cur_length,0)
  134.  
  135. void BoundedValue::scroll_to(DimensionName d, Coord position) {
  136.     Coord p = position;
  137.     constrain(d, p);
  138.     if (p != curvalue_) {
  139.     curvalue_ = p;
  140.     notify(Dimension_X);
  141.     notify(Dimension_Y);
  142.     }
  143. }
  144.  
  145. #define scroll_function(name,expr) \
  146. void BoundedValue::name(DimensionName d) { \
  147.     scroll_to(d, curvalue_ + expr); \
  148. }
  149.  
  150. scroll_function(scroll_forward,+scroll_incr_)
  151. scroll_function(scroll_backward,-scroll_incr_)
  152. scroll_function(page_forward,+page_incr_)
  153. scroll_function(page_backward,-page_incr_)
  154.  
  155. int main(int argc, char** argv) {
  156.     Session* session = new Session("Himom", argc, argv);
  157.     Kit* kit = Kit::instance();
  158.     Style* style = session->style();
  159.     BoundedValue* b = new BoundedValue(0.0, 100.0);
  160.     App* a = new App(b);
  161.     b->current_value(50.0);
  162.     b->scroll_incr(5.0);
  163.     b->page_incr(20.0);
  164.     Glyph* print_button = kit->simple_push_button(
  165.     "Print value", style, new ActionCallback(App)(a, &App::print_value)
  166.     );
  167.     Glyph* continuous_button = kit->simple_toggle_button(
  168.     "Continuous", style, new ActionCallback(App)(a, &App::continuous)
  169.     );
  170.     return session->run_window(
  171.     new ApplicationWindow(
  172.         kit->flat_frame(
  173.         new Margin(
  174.             new LRBox(
  175.             new TBBox(
  176.                 new Center(print_button, float(0.5), float(1.0)),
  177.                 new VGlue(10.0),
  178.                 new HCenter(continuous_button, 0.5)
  179.             ),
  180.             kit->inset_frame(kit->vscroll_bar(b, style), style)
  181.             ),
  182.             5.0
  183.         ),
  184.         style
  185.         )
  186.     )
  187.     );
  188. }
  189.